package com.uenpay.util.common;

import android.content.Context;
import android.content.pm.PackageManager;

import com.uenpay.util.app.VersionInfo;
import com.socks.library.KLog;
import com.uenpay.util.net.http.HostAddress;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * Created by liulou on 2017/4/12.
 * desc:
 */

public class AppConfigUtil {


    public static void init(Context context) {
        Properties properties = getAppCofigProperties(context);
        initHostAddr(properties);
        AppConfig.LOG_TAG = properties.getProperty("LOG_TAG", "UenPay");
        AppConfig.APP_LOCAL_FILE_NAME = properties.getProperty("LOCAL_FILE_NAME", "UenPay");
        AppConfig.IS_RELEASE = Boolean.parseBoolean(properties.getProperty("IS_RELEASE", "false"));
        initVersionInfo(context, properties);

    }

    /**
     * 得到appconfig.properties中配置信息
     *
     * @return
     */
    private static Properties getAppCofigProperties(Context context) {
        Properties props = new Properties();
        try {
            InputStream in = context.getAssets().open("appconfig.properties");
            BufferedReader bf = new BufferedReader(new InputStreamReader(in));
            props.load(bf);
            bf.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return props;
    }

    /**
     * 初始化网络地址信息
     *
     * @param prop
     */
    private static void initHostAddr(Properties prop) {
        String ip = prop.getProperty("IP");
        int amsPort = Integer.parseInt(prop.getProperty("AMS_PORT"));
        int rmsPort = Integer.parseInt(prop.getProperty("RMS_PORT"));
        boolean isHttps = Boolean.parseBoolean(prop.getProperty("IS_HTTPS"));
        KLog.d(String.format("IP:%s  Ams Port:%d Rms Port:%d", ip, amsPort, rmsPort));
        AppConfig.HOST_ADDR = new HostAddress(ip, amsPort, rmsPort, isHttps);
    }

    /**
     * 初始化版本信息
     *
     * @param prop
     */
    private static void initVersionInfo(Context context, Properties prop) {
        String appName = prop.getProperty("APP_NAME");
        boolean isCheckVersion = Boolean.parseBoolean(prop.getProperty("IS_CHECK_VERSION"));
        String checkVerName = prop.getProperty("CHECK_VERSION_NAME");
        int versionCode = getVersionCode(context);
        String versionName = getVersionName(context);
        KLog.d(String.format("APP_NAME:%s  VERSION_CODE:%d VERSION_NAME:%s CHECK_VERSION_NAME:%s IS_CHECK_VERSION:%b", appName, versionCode, versionName, checkVerName, isCheckVersion));
        AppConfig.VERSION_INFO = new VersionInfo(appName, isCheckVersion, versionCode, versionName, checkVerName);
    }

    /**
     * 得到当前版本号
     *
     * @param context
     * @return
     */
    public static int getVersionCode(Context context) {
        int versionCode = 0;
        try {
            versionCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    /**
     * 得到当前版本号描述
     *
     * @param context
     * @return
     */
    public static String getVersionName(Context context) {
        String versionName = null;
        try {
            versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }
}
